package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.JsonResponse;
import edu.upc.clase.demo.entity.Instrumento;
import edu.upc.clase.demo.service.InstrumentoService;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Lorilei
 */

@Controller("InstrumentoController")
public class InstrumentoController {

    @Autowired
    private InstrumentoService InstrumentoService;
    private static Logger log = LoggerFactory.getLogger(InstrumentoController.class);

    @RequestMapping("/Registrar Instrumentos/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("Registrar Instrumentos/index");
        List<Instrumento> instrumentos = InstrumentoService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("instrumentos", instrumentos);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
    @RequestMapping(value = "/Registrar Instrumentos/new", method = RequestMethod.GET)
    public ModelAndView newInstrumento() {
        ModelAndView mav = new ModelAndView("Registrar Instrumentos/new");
        Instrumento instrumento = new Instrumento();
        mav.getModelMap().put("Instrumento", instrumento);
        return mav;
    }

    @RequestMapping(value = "/Registrar Instrumentos/new", method = RequestMethod.POST)
    public String createUsuario(@ModelAttribute("Instrumento")Instrumento instrumento, 
                                SessionStatus status,
                                HttpServletRequest request) {
        
        InstrumentoService.insertar(instrumento);
           
        status.setComplete();

        return "redirect:/pages/Registrar Instrumentos/index";
    }
    
    
    @RequestMapping(value = "/Registrar Instrumentos/edit", method = RequestMethod.GET)
    public ModelAndView editInstrumento(@RequestParam("id")String codInst) {    
        ModelAndView mav = new ModelAndView("Registrar Instrumentos/edit");
        Instrumento instrumento = InstrumentoService.buscar(codInst);
        mav.getModelMap().put("Instrumento", instrumento);
        return mav;        
    }
    
    @RequestMapping(value="/Registrar Instrumentos/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("Instrumento") Instrumento instrumento, SessionStatus status) {
        InstrumentoService.actualizar(instrumento);
        status.setComplete();
        return "redirect:/pages/Registrar Instrumentos/index";
    }
    
    @RequestMapping("/Registrar Instrumentos/delete")
    public ModelAndView delete(@RequestParam("id")String codInst)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/Registrar Instrumentos/index");
        Instrumento instrumento = InstrumentoService.buscar(codInst);
        InstrumentoService.eliminar(instrumento);
        return mav;
    }
    
    @RequestMapping(value = "/Instrumento/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("Instrumento/resultadoBusqueda");
        log.info("resultado = " + InstrumentoService.buscarPorNombre(criterio.getNombre()).size());
        mav.getModel().put("resultado",InstrumentoService.buscarPorNombre(criterio.getNombre()));
        return mav;
    }

}
